<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="类型名称" prop="typeName">
            <el-input
                v-model="queryParams.typeName"
                placeholder="请输入类型名称"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['media:mediaType:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['media:mediaType:remove']"
            >删除
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="mediaTypeList" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="ID"  key="id" prop="id" align="center" v-if="columns[0].visible"/>
          <el-table-column label="分类名称"  key="typeName" prop="typeName" align="center" v-if="columns[1].visible"/>
          <el-table-column label="状态"  key="status" align="center" v-if="columns[2].visible">
            <template #default="scope">
              <el-switch
                  v-model="scope.row.status"
                  active-value="1"
                  inactive-value="0"
                  @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[3].visible" width="160">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                           v-hasPermi="['media:mediaType:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" ref="mediaTypeRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MediaType">
import {
  listMediaType,
  changeMediaTypeStatus,
  getMediaType,
  addMediaType,
  updateMediaType,
  delMediaType
} from "@/api/system/mediaType";

const {proxy} = getCurrentInstance();
const {sys_normal_disable, sys_user_sex} = proxy.useDict("sys_normal_disable", "sys_user_sex");

const mediaTypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 列显隐信息
const columns = ref([
  {key: 0, label: `ID`, visible: true},
  {key: 1, label: `分类名称`, visible: true},
  {key: 2, label: `状态`, visible: true},
  {key: 3, label: `创建时间`, visible: true}
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    typeName: undefined
  }
});

const {queryParams, form} = toRefs(data);

/** 查询用户列表 */
function getList() {
  loading.value = true;
  listMediaType(queryParams.value).then(res => {
    loading.value = false;
    mediaTypeList.value = res.rows;
    total.value = res.total;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.regionId = undefined;
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除编号为"' + Ids + '"的数据项？').then(function () {
    return delMediaType(Ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}


/** 状态修改  */
function handleStatusChange(row) {
  let text = row.status === "0" ? "停用" : "启用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.typeName + '"吗?').then(function () {
    return changeMediaTypeStatus(row.id, row.status);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "0" ? "1" : "0";
  });
}


/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 重置操作表单 */
function reset() {
  form.value = {
    typeName: undefined,
  };
  proxy.resetForm("mediaTypeRef");
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getMediaType().then(response => {
    open.value = true;
    title.value = "添加分类";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mediaTypeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateMediaType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMediaType(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

getList();
</script>
